package com.danceforyou.customer.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.OperationService;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.customer.dao.CustomerUserDAO;
import com.danceforyou.customer.model.CustomerDO;
import com.danceforyou.customer.model.CustomerUserDO;
import com.danceforyou.customer.service.CustomerService;
import com.danceforyou.customer.service.CustomerUserService;
import com.danceforyou.customer.vo.CustomerUserVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
* 类名称：CustomerUserServiceImpl
* 创建人：danceforyou
* 创建时间：2021年02月03日
* 版本号：1.0v
*/
@Service("customerUserService")
public class CustomerUserServiceImpl extends BaseServiceImpl<Long, CustomerUserDO, CustomerUserVO, CustomerUserDAO> implements CustomerUserService {
    @Autowired
    private CustomerUserDAO customerUserDAO;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private OperationService operationService;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public CustomerUserDAO getDao() {
        return customerUserDAO;
    }

    @Override
    public void saveCustomerUser(Long customerId, String userIds) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        CustomerDO customer = customerService.getById(customerId);
        AssertUtil.notNull(customer, "客户信息不存在");
        AssertUtil.isTrue(Constants.ROLE_GLB_CODE.equals(loginUser.getRole().getRoleType()) || customer.getCreateUserId().equals(loginUser.getId()), "非法操作");
        String[] userIdArr = userIds.split(",");
        //获取原客户授权员工
        List<CustomerUserDO> customerUserList = this.listByProperty("customerId", customerId);
        for (String userId : userIdArr) {
            boolean isAuthed = false;
            for (CustomerUserDO customerUser : customerUserList) {
                if (userId.equals(customerUser.getBelongUserId().toString())) {
                    isAuthed = true;
                    customerUserList.remove(customerUser);
                    break;
                }
            }
            //如果该用户还未授权，则保存授权信息
            if (!isAuthed) {
                CustomerUserDO customerUser = new CustomerUserDO();
                customerUser.setCustomerId(customerId);
                customerUser.setBelongUserId(Long.parseLong(userId));
                customerUser.setAuthUserId(loginUser.getId());
                customerUser.setAuthDate(new Date());
                this.insert(customerUser);
            }
        }
        //删除取消授权的用户
        for (CustomerUserDO customerUser : customerUserList) {
            this.delete(customerUser);
        }
        operationService.saveOperation(Constants.OPER_TABLE_CUSTOMER, customer.getId(), Constants.OPER_AUTH, loginUser, userIds);
    }
}
